package bowlingScorekeeper;
import java.awt.EventQueue;

import javax.swing.JFrame;

import java.awt.GridLayout;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
import javax.swing.BoxLayout;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.Dimension;

import javax.swing.SwingConstants;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.border.LineBorder;

/**
 * The class that displays the game and takes input
 * 
 * @author Mark Wiggans
 */
public class bowlingGame {
	
	/**
	 * Holds all of the colors for the program
	 * 0 = Background Color
	 * 1 = PlayerName Background and Fina Score Background
	 * 2 = Playername Text Color and Final Score Text Background
	 * 3 = Frame Top Background
	 * 4 = Frame Top Text Color
	 * 5 = Frame Bottom Background
	 * 6 = Frame Bottom Text Color
	 */
	Color[] colors;

	/**
	 * The frame that everything is put into 
	 */
	private JFrame frame;

	/**
	 * Launches the application
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					bowlingGame window = new bowlingGame();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	// Ensures that the player actually has a name
	private String playerName = "Mark";

	/**
	 * Creates the application.
	 */
	public bowlingGame() {
		initialize();
		colors = new Color[] {
				Color.blue, Color.blue, Color.white, Color.cyan, Color.black, Color.blue, Color.white
		};
	}

	/**
	 * Creates the application with names from nameInput
	 */
	public bowlingGame(String playerNameIn) {
		playerName = playerNameIn;
		initialize();
		frame.setVisible(true);
		colors = new Color[] {
				Color.blue, Color.blue, Color.white, Color.cyan, Color.black, Color.blue, Color.white
		};
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {

		// Creates the window and sets size
		frame = new JFrame();
		frame.setMinimumSize(new Dimension(600, 175));
		frame.setBounds(100, 100, 901, 211);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		// Creates the number line across the top
		final numberRow topReferenceNumbers = new numberRow();
		frame.add(topReferenceNumbers);

		// Creates the container that stores the score tables
		final JPanel scoreContainer = new JPanel();
		scoreContainer.setBounds(10, 25, frame.getSize().width - 40,
				frame.getSize().height - 70);
		frame.getContentPane().add(scoreContainer);
		// scoreContainer.setLayout(new GridLayout(1, 0, 0, 0));
		scoreContainer.setLayout(null);

		// Creates the game frame
		final game newGame = new game();
		newGame.setBounds(0, 0, 0, 0);
		scoreContainer.add(newGame);

		// Resizes internal components when entire frame is resized
		frame.addComponentListener(new ComponentListener() {

			@Override
			public void componentShown(ComponentEvent arg0) {
			}

			@Override
			public void componentResized(ComponentEvent arg0) {
				System.out.println("Height: " + frame.getHeight() + " Width: "+ frame.getWidth());
				scoreContainer.setBounds(10, 25, frame.getSize().width - 35,frame.getSize().height - 70);
				newGame.setBounds(0, 0, scoreContainer.getSize().width, 100);
				topReferenceNumbers.setBounds(10, 10,frame.getSize().width - 35, 15);
			}

			@Override
			public void componentMoved(ComponentEvent arg0) {
				
			}

			@Override
			public void componentHidden(ComponentEvent arg0) {
				
			}
		});

		
		frame.addKeyListener(new KeyListener() {

			public void keyTyped(KeyEvent e) {
				if (e.getKeyChar() == '0' || e.getKeyChar() == '1'
						|| e.getKeyChar() == '2' || e.getKeyChar() == '3'
						|| e.getKeyChar() == '4' || e.getKeyChar() == '5'
						|| e.getKeyChar() == '6' || e.getKeyChar() == '7'
						|| e.getKeyChar() == '8' || e.getKeyChar() == '9'
						|| e.getKeyChar() == '*' || e.getKeyChar() == '/'
						|| e.getKeyChar() == '-') {
					// newGame.addData(e.getKeyChar());
					newGame.tracker.addData(e.getKeyChar());
				}

			}

			@Override
			public void keyReleased(KeyEvent e) {

			}

			@Override
			public void keyPressed(KeyEvent e) {
			}
		});
	}

	/**
	 * The class that holds the game and all of its data
	 * 
	 * @author Mark Wiggans
	 *
	 */
	public class game extends JPanel {
		
		
		frame[] frameTable;
		private finalFrame finalFrame;
		
		private int[][] data = new int[10][4];
		// [0] is the first roll
		// [1] is the second roll
		// [2] is the third roll
		// [3] is the subtotal

		final public gameTracker tracker = new gameTracker();

		game() {
			init();
		}

		private void init() {
			setBackground(Color.BLUE);
			setLayout(new BorderLayout(0, 0));

			JPanel letterName = new JPanel();
			letterName.setBackground(Color.BLUE);
			letterName.setPreferredSize(new Dimension(100, 10));
			add(letterName, BorderLayout.WEST);

			JLabel playerLabel = new JLabel(playerName.charAt(0) + "");
			playerLabel.setForeground(Color.WHITE);
			playerLabel.setBackground(SystemColor.activeCaptionBorder);
			playerLabel.setSize(new Dimension(100, 0));
			playerLabel.setFont(new Font("Tahoma", Font.PLAIN, 45));
			letterName.add(playerLabel);

			JPanel finalScoreContainer = new JPanel();
			finalScoreContainer.setBackground(Color.BLUE);
			finalScoreContainer.setPreferredSize(new Dimension(100, 10));
			add(finalScoreContainer, BorderLayout.EAST);

			JLabel finalScore = new JLabel("Final");
			finalScore.setAlignmentY(Component.TOP_ALIGNMENT);
			finalScore.setVerticalTextPosition(SwingConstants.BOTTOM);
			finalScore.setVerticalAlignment(SwingConstants.BOTTOM);
			finalScore.setFont(new Font("Tahoma", Font.PLAIN, 45));
			finalScore.setForeground(Color.WHITE);
			finalScoreContainer.add(finalScore);

			JPanel frameContainer = new JPanel();
			add(frameContainer, BorderLayout.CENTER);
			frameContainer.setLayout(new GridLayout(1, 1, 0, 0));

			frameTable = new frame[9];
			finalFrame = new finalFrame();
			for (int count = 0; count < 9; count++) {
				frameTable[count] = new frame();
				frameContainer.add(frameTable[count]);
			}
			frameContainer.add(finalFrame);
		}

		class frame extends JPanel {
			/**
			 * These can represent the first nine frames of a game
			 */
			private static final long serialVersionUID = 1L;
			
			/**
			 * Default constructor
			 */
			frame() {
				init();
			}
			
			public JLabel firstBall;
			public JLabel secondBall;
			private JLabel subtotalLabel;
			private void init() {
				setBorder(new LineBorder(new Color(0, 0, 0)));
				setLayout(new GridLayout(0, 1, 0, 0));

				JPanel frameOneIndividualContainer = new JPanel();
				frameOneIndividualContainer.setBackground(Color.CYAN);
				add(frameOneIndividualContainer);
				frameOneIndividualContainer.setLayout(new GridLayout(1, 0, 0, 0));

				// setLayout(new GridLayout(1, 0, 0, 0));

				firstBall = new JLabel("");
				firstBall.setFont(new Font("Tahoma", Font.PLAIN, 20));
				firstBall.setHorizontalAlignment(SwingConstants.CENTER);
				frameOneIndividualContainer.add(firstBall);

				secondBall = new JLabel("");
				secondBall.setHorizontalAlignment(SwingConstants.CENTER);
				secondBall.setFont(new Font("Tahoma", Font.PLAIN, 20));
				frameOneIndividualContainer.add(secondBall);

				JPanel subtotal = new JPanel();
				subtotal.setBackground(Color.BLUE);
				add(subtotal);

				JLabel subtotalLabel = new JLabel("");
				subtotalLabel.setForeground(Color.WHITE);
				subtotalLabel.setBackground(Color.WHITE);
				subtotalLabel.setHorizontalAlignment(SwingConstants.CENTER);
				subtotalLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
				subtotal.add(subtotalLabel);
			}
			public void setSubtotal(int i) {
				subtotalLabel.setText(i+"");
				
			}
		}

		class finalFrame extends JPanel {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			finalFrame() {
				init();
			}

			void init() {
				// JPanel frameOne = new JPanel();
				setBorder(new LineBorder(new Color(0, 0, 0)));
				// add(frameOne);
				setLayout(new GridLayout(0, 1, 0, 0));

				JPanel frameOneIndividualContainer = new JPanel();
				frameOneIndividualContainer.setBackground(Color.CYAN);
				add(frameOneIndividualContainer);
				frameOneIndividualContainer.setLayout(new GridLayout(1, 0, 0, 0));

				// setLayout(new GridLayout(1, 0, 0, 0));

				JLabel firstBall = new JLabel("1");
				firstBall.setFont(new Font("Tahoma", Font.PLAIN, 20));
				firstBall.setHorizontalAlignment(SwingConstants.CENTER);
				frameOneIndividualContainer.add(firstBall);

				JLabel secondBall = new JLabel("2");
				secondBall.setHorizontalAlignment(SwingConstants.CENTER);
				secondBall.setFont(new Font("Tahoma", Font.PLAIN, 20));
				frameOneIndividualContainer.add(secondBall);

				JLabel thirdBall = new JLabel("3");
				thirdBall.setHorizontalAlignment(SwingConstants.CENTER);
				thirdBall.setFont(new Font("Tahoma", Font.PLAIN, 20));
				frameOneIndividualContainer.add(thirdBall);

				JPanel subtotal = new JPanel();
				subtotal.setBackground(Color.BLUE);
				add(subtotal);

				JLabel label_10 = new JLabel("S");
				label_10.setForeground(Color.WHITE);
				label_10.setBackground(Color.WHITE);
				label_10.setHorizontalAlignment(SwingConstants.CENTER);
				label_10.setFont(new Font("Tahoma", Font.BOLD, 20));
				subtotal.add(label_10);
			}
		}

		class gameTracker {
			private int currentFrame;
			private int currentRoll;
			private int pointsInLastLastRoll;

			public gameTracker() {
				currentFrame = 0;
				currentRoll = 0;
				pointsInLastLastRoll = -1; //Shouldn't matter but just to make sure
				for(int count = 0; count < 10; count++){
					for(int count2 = 0; count2 < 4; count2++){
						data[count][count2] = -1;
					}
				}
			}

			public int getCurrentFrame() {
				return currentFrame;
			}

			public int getCurrentRoll() {
				return currentRoll;
			}

			public void addData(char rollData) {
				int CurrentRollInt = 0;
				try{
					CurrentRollInt= Integer.parseInt(rollData+"");
				}catch(Exception e){
					System.out.println("Put in a non-number character");
				}
				if(currentFrame != 9){
					if(currentRoll == 0){
						if(CurrentRollInt < 10 && CurrentRollInt > 0){ //General Case
							data[currentFrame][currentRoll] = Integer.parseInt(rollData+"");
							frameTable[currentFrame].firstBall.setText(data[currentFrame][currentRoll]+"");
							currentRoll++;
							pointsInLastLastRoll = CurrentRollInt;
							
						}else if(rollData == '0' || rollData == '-'){ //Special case
							data[currentFrame][currentRoll] = 0;
							frameTable[currentFrame].firstBall.setText("-");
							currentRoll++;
							pointsInLastLastRoll = CurrentRollInt;
							
						}else if(rollData == 'X'||rollData == 'x'||rollData=='*'||rollData == '/'){
							data[currentFrame][currentRoll] = 10;
							frameTable[currentFrame].firstBall.setText("X");
							currentFrame++;
						}
						
					}else	if(currentRoll == 1){
						if(CurrentRollInt < 10-pointsInLastLastRoll && CurrentRollInt > 0){ //General Case
							data[currentFrame][currentRoll] = Integer.parseInt(rollData+"");
							frameTable[currentFrame].secondBall.setText(data[currentFrame][currentRoll]+"");
							currentRoll = 0;
							currentFrame ++;
							
						}else if(rollData == '0' || rollData == '-'){ //Special case
							data[currentFrame][currentRoll] = 0;
							frameTable[currentFrame].secondBall.setText("-");
							currentRoll++;
							pointsInLastLastRoll = data[currentFrame][currentRoll];
							currentRoll = 0;
							currentFrame ++;
							
						}else if(rollData == 'X'||rollData == 'x'||rollData=='*'||rollData == '/'||Integer.parseInt(rollData+"") == 10-pointsInLastLastRoll ){
							data[currentFrame][currentRoll] = 10-data[currentFrame][0];
							frameTable[currentFrame].secondBall.setText("/");
							currentRoll = 0;
							currentFrame ++;
						}
					}
				}else{
					
				}
				updateSubtotals();
			}
			public void updateSubtotals(){
				for(int count = 0; count < 9; count ++){
					if(data[count][3] == -1){ //If there is no subtotal for a frame
						if(count == 9){
							if(data[count][0] == 10){ //If a strike is scored in that frame

							}
							else if(data[count][0]+data[count][1] == 10){ //If scored a spare
								if(data[count+1][0] != -1){ //Ensures that there is data in the next 
									
								}
								if(count == 0){
									data[count][3] = data[count-1][3] + 10 + data[count+1][0];
								}else if(count == 9){
									
								}else{
									data[count][3] = data[count-1][3] + 10 + data[count+1][0];
								}
								
								frameTable[count].setSubtotal(data[count][3]);
							}else{ //If didn't mark
								if(count == 9){
									data[count][3] = data[count][0] + data[count][1] + data[count][2];
									frameTable[count].setSubtotal(data[count][3]);
								}else{
									
								}
								
							}
						}else{
							if(data[count][0] == 10){ //If a strike is scored in that frame
								
								if(data[count+1][0] == 10){ //If a strike is scored in the next frame
									
								}
								
							}
							//-----------------------------------------------------------------------------------------------
							else if(data[count][0]+data[count][1] == 10){ //If scored a spare
								if(data[count+1][0] != -1){ //Ensures that there is data in the next 
									
								}
								if(count == 0){
									data[count][3] = data[count-1][3] + 10 + data[count+1][0];
								}else if(count == 9){
									
								}else{
									data[count][3] = data[count-1][3] + 10 + data[count+1][0];
								}
								
								frameTable[count].setSubtotal(data[count][3]);
							}else{ //If didn't mark
								if(count == 0){
									data[count][3] = data[count][0] + data[count][1];
									frameTable[count].setSubtotal(data[count][3]);
								}else{
									
								}
								
							}
						}
					}
				}
				
			}
		}
	}

	/**
	 * Creates the numbers that mark the rows
	 * 
	 * @author Mark Wiggans
	 */
	public class numberRow extends JPanel {
		private static final long serialVersionUID = 1L;

		numberRow() {

			setLayout(new BorderLayout(0, 0));

			JPanel panel_36 = new JPanel();
			panel_36.setPreferredSize(new Dimension(100, 10));
			FlowLayout flowLayout = (FlowLayout) panel_36.getLayout();
			flowLayout.setAlignment(FlowLayout.LEFT);
			add(panel_36, BorderLayout.WEST);

			JPanel panel_37 = new JPanel();
			panel_37.setPreferredSize(new Dimension(100, 10));
			add(panel_37, BorderLayout.EAST);

			JPanel panel_38 = new JPanel();
			add(panel_38, BorderLayout.CENTER);
			panel_38.setLayout(new GridLayout(1, 0, 0, 0));

			for (int count = 1; count < 11; count++) {
				JPanel panel_39 = new JPanel();
				panel_38.add(panel_39);
				panel_39.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 0));

				JLabel label = new JLabel(count + "");
				label.setAlignmentY(Component.TOP_ALIGNMENT);
				label.setVerticalAlignment(SwingConstants.TOP);
				label.setFont(new Font("Tahoma", Font.BOLD, 12));
				panel_39.add(label);
			}
		}
	}
}
